package cn.ticket.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Carpoolcategory;
import cn.ticket.model.Carpooling;
import cn.ticket.service.CarPoolingCategoryService;
import cn.ticket.service.CarPoolingService;
import cn.ticket.service.Utility;

@Controller
@RequestMapping("carpooling")
public class CarPoolingController {
@Autowired
CarPoolingService carPoolingService;
@Autowired
CarPoolingCategoryService carPoolingCategoryService;
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Carpooling record){
	Map<String, Object> result=new HashMap<>();
	int num = carPoolingService.insertSelective(record);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));
	int staffId=Integer.parseInt(args.get("staffId"));
	Carpooling carpooling=carPoolingService.selectByPrimaryKey(id);
	if (carpooling.getStaffid()!=staffId) {
		result.put("status", 1002);
	}
	int num = carPoolingService.deleteByPrimaryKey(id);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getValidateCarPooling",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getValidateCarPooling(){
	Map<String, Object> result=new HashMap<>();
	List<Map<String, Object>> carpoolinfo=carPoolingService.getValidateCarPooling();
	if (!carpoolinfo.isEmpty()) {
		result.put("status", 1001);
		result.put("body", carpoolinfo);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getCategory",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getCategory(){
	Map<String, Object> result=new HashMap<>();
	List<Carpoolcategory> carpoolcategories=carPoolingCategoryService.getAll();
	if (!carpoolcategories.isEmpty()) {
		result.put("status", 1001);
		result.put("body", carpoolcategories);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
}
